package com.bird.producer;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * @author sijunqiang
 * @data 2021/7/18 16:36
 * @description: 自定义kafka的partition的负载均衡
 */
public class SimplePartition implements Partitioner {

    /**
     * 实现的功能是：根据key的不同 将消息发送到不同的partition.
     * 默认是：到0这个分区
     *
     * @Reture: 返回的是指定的哪个分区
     */
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        String keyStr = (String) key;
        String keyInt = keyStr.substring(4);
        int partition = Integer.parseInt(keyInt);
        System.out.println("hello-partition");
        return partition % 2;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
